package DesignPatern.A02_工厂模式.QXB.A04_延迟加载工厂方法;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Du
 * @Date: 2021/7/20 20:01
 */
public class ProductFactory
{
    private static final Map<String, Product> productMap = new HashMap<>();
    
    public static synchronized Product createProduct(String type)
    {
        Product product = null;
        if (productMap.containsKey(type))
        {
            product = productMap.get(type);
            System.out.println("从缓存工厂中取出Product");
        } else
        {
            if (type.equalsIgnoreCase("Product1"))
            {
                product = new ConcreteProduct1();
            } else
            {
                product = new ConcreteProduct2();
            }
            productMap.put(type, product);
        }
        return product;
    }
}
